<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Orders extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('good_m');
        $this->load->model('shop_m');
        $this->load->model('order_m');
        $this->load->model('user_m');
        $this->load->model('delivers_m');
        $this->load->model('message_m');
        $this->load->library('pagination');

        $this->load->model('category_m');
        $this->load->model('member_m');

        $this->load->model('tmporder_m');
        $this->uid = $this->session->userdata('user_id');
    }

    /**
     * 列表
     * [index description]
     * @return [type] [description]
     */
    public function index($page = 1)
    {

        $limit                 = 20;
        $config['uri_segment'] = 4;
        // $page = !empty($config['uri_segment'])?$config['uri_segment']:'0';
        $config['use_page_numbers'] = true;
        $config['base_url']         = site_url('manage/orders/index/');
        if ($this->auth->is_super_admin()) {
            $config['total_rows'] = $this->db->count_all('shop_orders');
        } else {
            $config['total_rows'] = $this->db->where("user_id", $this->uid)->count_all('shop_orders');
        }
        $config['per_page']  = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start              = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        if ($this->auth->is_super_admin()) {
            $orders = $this->order_m->get_all_orders($start, $limit);
        } else {
            $orders = $this->order_m->get_all_orders($start, $limit, $this->uid);
        }
        if (!empty($orders)) {
            foreach ($orders as $key => $val) {
                $goodinfo                   = $this->good_m->get_shop_goods_by_id($val['good_id']);
                $shopinfo                   = $this->shop_m->get_shop_user_by_id($val['shop_id']);
                $userinfo                   = $this->user_m->get_user_by_user_id($val['user_id']);
                $orders[$key]['good_title'] = $goodinfo['good_title'];
                $orders[$key]['shop_title'] = $shopinfo['shop_title'];
                $orders[$key]['user_name']  = $userinfo['user_name'];
            }
        }
//        pp($orders);exit();
        $data['orders'] = $orders;
        $this->load->view('orders/index', $data);
    }

    //查询已发货的订单
    public function order_status($page = 1)
    {
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['base_url']         = site_url('manage/orders/order_status');
        $config['total_rows']       = $this->order_m->count_all_ings();
        $config['per_page']         = $limit;
        $config['num_links']        = 10;
        $this->pagination->initialize($config);
        $start              = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        $orders             = $this->order_m->get_delivers($start, $limit);
        if (!empty($orders)) {
            foreach ($orders as $key => $val) {
                $goodinfo                   = $this->good_m->get_shop_goods_by_id($val['good_id']);
                $shopinfo                   = $this->shop_m->get_shop_user_by_id($val['shop_id']);
                $userinfo                   = $this->user_m->get_user_by_user_id($val['user_id']);
                $orders[$key]['good_title'] = $goodinfo['good_title'];
                $orders[$key]['shop_title'] = $shopinfo['shop_title'];
                $orders[$key]['user_name']  = $userinfo['user_name'];
            }
        }
        $data['orders'] = $orders;
        $this->load->view('orders/index', $data);
    }

    //发货
    public function delivers($id)
    {
        if ($this->input->post()) {
            $data['order_id']          = $id;
            $deliversid                = $this->input->post('deliver_company');
            $data['deliver_no']        = $this->input->post('deliver_no');
            $data['deliver_send_time'] = time();
            $deliver                   = $this->delivers_m->get_delivers_by_id($deliversid);
            $data['deliver_company']   = $deliver['deliver_name'];
            $data['deliver_code']      = $deliver['deliver_code'];
            $result                    = $this->delivers_m->add_deliver($data);
            if ($result) {
                //更改物流状态为已发货
                $arr['order_deliver'] = 2;
                $this->order_m->sava_shop_orders($id, $arr);
                $order_info = $this->order_m->get_shop_orders_by_id($id);
                $order_no   = $order_info['order_no'];
                //提现信息存到message表中
                $jlmsg['message_txt']    = '发货成功，您的订单<em>' . $order_no . '</em>已发货。<a href="' . site_url("member/order_logistics/" . $id) . '">查看物流</a>';
                $jlmsg['message_type']   = 10;
                $jlmsg['message_to_uid'] = $order_info['user_id'];
                $jlmsg['message_time']   = time();
                $jlmsg['message_lock']   = 1;
                $this->message_m->add_massage($jlmsg);
                show_message('发货成功！', site_url(X_URL . 'orders/index'), 1);
            } else {
                show_message('发货失败重新发货', site_url(X_URL . 'orders/orders/' . $id), 1);
            }
        }
        $data['delivers'] = $this->delivers_m->get_all_delivers_comenpty();
        $data['id']       = $id;
        $this->load->view('orders/delivers', $data);
    }

    //修改发货
    public function edit_delivers($id = 0)
    {
        if ($this->input->post()) {
            $deliver_id                = $this->input->post('deliver_id');
            $deliversid                = $this->input->post('deliver_company');
            $data['deliver_no']        = $this->input->post('deliver_no');
            $data['deliver_send_time'] = time();
            $deliver                   = $this->delivers_m->get_delivers_by_id($deliversid);
            $data['deliver_company']   = $deliver['deliver_name'];
            $data['deliver_code']      = $deliver['deliver_code'];
            $result                    = $this->delivers_m->save_deliver($deliver_id, $data);
            if ($result) {
                //更改物流状态为已发货
                $arr['order_deliver'] = 2;
                $this->order_m->sava_shop_orders($id, $arr);
                show_message('修改发货成功！', site_url(X_URL . 'orders/index'), 1);
            }
        }
        //根据订单的id查询用户的物流信息
        $data['info']     = $this->delivers_m->get_deliver_by_order_id($id);
        $data['delivers'] = $this->delivers_m->get_all_delivers_comenpty();
        $this->load->view('orders/edit_delivers', $data);
    }

    //查询所有未发货的订单
    public function ordering($page = 1)
    {
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['base_url']         = site_url('manage/orders/ordering/');
        $config['total_rows']       = $this->order_m->count_all_ing();
        $config['per_page']         = $limit;
        $config['num_links']        = 10;
        $this->pagination->initialize($config);
        $start              = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        $orders             = $this->order_m->get_delivering($start, $limit);
        if (!empty($orders)) {
            foreach ($orders as $key => $val) {
                $goodinfo                   = $this->good_m->get_shop_goods_by_id($val['good_id']);
                $shopinfo                   = $this->shop_m->get_shop_user_by_id($val['shop_id']);
                $userinfo                   = $this->user_m->get_user_by_user_id($val['user_id']);
                $orders[$key]['good_title'] = $goodinfo['good_title'];
                $orders[$key]['shop_title'] = $shopinfo['shop_title'];
                $orders[$key]['user_name']  = $userinfo['user_name'];
            }
        }
        $data['orders'] = $orders;
        $this->load->view('orders/index', $data);
    }

    //删除失败的订单
    public function del($id)
    {
        $results = $this->order_m->del_order_by_id($id);
        if ($results) {
            show_message('删除订单成功', site_url(X_URL . 'orders/index'), 1);
        }
    }

    //查询订单
    public function delivers_show($order_id)
    {
        $data['order_info'] = $this->order_m->get_shop_orders_by_id($order_id);
        $this->load->view('orders/deliver_show', $data);
    }

    /**
     * 发货和支付
     * @param $order_id
     * @return bool
     */
    public function get_status($order_id, $status)
    {

        if (!$order_id) {
            return false;
        }
        $info = $this->order_m->get_shop_orders_by_id($order_id);
        if (!empty($info)) {
            $return = false;
            if ($status == 1) {
                // 确认收款
                $return = $this->order_m->sava_shop_orders($order_id, ['order_pay' => 1, 'order_status' => 1]);
            }
            if ($status == 2) {
                // 发货
                $return  = $this->order_m->sava_shop_orders($order_id, ['order_status' => 2]);
                $massage = [
                    'message_txt'    => "您的订单已经安排送货啦！",
                    'message_to_uid' => $order_id,
                    'message_time'   => date("Y-m-d H:i:s"),
                ];
                $this->message_m->add_massage($massage);
            }
            if ($return) {
                show_message('操作订单成功', site_url(X_URL . 'orders/index'), 1);
            } else {
                show_message('操作订单成功', site_url(X_URL . 'orders/index'), 1);
            }
        }
    }

    /***************************************用户下单*******************************************/

    public function create()
    {

        if ($_POST) {
            $len = count($_POST['category_id']);
            for ($i = 0; $i < $len; $i++) {
                foreach ($_POST as $key => $value) {
                    $data[$key] = $value[$i];
                }
                $orders[] = $data;
            }
            $order_no = $this->create_order_no();
            if (!empty($orders)) {
                $order_price = "0";
                foreach ($orders as $key => $val) {
                    $orders[$key]['user_id'] = $this->uid;
                    // 根据商品id获取商品信息
                    $good                           = $this->good_m->get_shop_goods_by_id($val['product_id']);
                    $good_total_price               = $val['good_mum'] * ($good['good_price'] * 100) / 100;
                    $orders[$key]['good_price']     = $good_total_price;
                    $order_price                    += $good_total_price;
                    $orders[$key]['order_add_time'] = time();
                    $orders[$key]['order_no']       = $order_no;
                    $this->tmporder_m->add_tmp_orders($orders[$key]);
                }
                // 总订单表记录
                $service_fee               = $order_price * ($this->config->item('service_tax') * 100) / 100;
                $order_data                = [
                    'order_no'       => $order_no,
                    'user_id'        => $this->uid,
                    'good_price'     => $order_price,
                    'order_add_time' => time(),
                ];
                $order_data['service_fee'] = $service_fee;
                $order_data['order_price'] = $order_price + $service_fee;
                $order_id                  = $this->order_m->create_order($order_data);
                $arr                       = [
                    [
                        "order_no" => $order_no,
                    ],
                    [
                        "order_no" => $order_no,
                        "order_id" => $order_id,
                    ],
                ];
                $this->db->update_batch("tmp_orders", $arr, "order_no");
                show_message('操作订单成功', site_url(X_URL . 'orders/pay_order/' . $order_no), 1);
            }
        }
        // 获取默认地址
        $dress = $this->member_m->get_adress_by_user_id($this->uid);
        if (!empty($dress)) {
            $area               = $this->areas_m->get_area_by_id($dress['dress_province']);
            $area2              = $this->areas_m->get_area_by_id($dress['dress_city']);
            $area3              = $this->areas_m->get_area_by_id($dress['dress_district']);
            $data['dress_city'] = $area['area_name'] . $area2['area_name'] . $area3['area_name'] . $dress['dress_udress'];
        } else {
            $data['dress_city'] = "请选择收货地址";
        }
        // 获取所有的产品类型
        $data['cates']    = $this->category_m->get_cates_by_pid(0);
        $data['dress_id'] = $dress['dress_id'];
        $this->load->view('orders/create', $data);
    }

    /**
     * 创建订单号
     * @return string
     */
    public function create_order_no()
    {
        $order_number = date('Ymd') . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        return $order_number;
    }

    public function pay_order($order_no)
    {

        if (!$order_no) {
            return false;
        }
        // 根据订单号获取订单
        $order = $this->order_m->get_shop_orders_by_order_no($order_no);
        if ($order['order_pay'] == 1) {
            show_message('订单已经支付了哦！', site_url(X_URL . 'orders/index/'), 1);
            exit();
        }
        $data['order']  = $order;
        $member         = $this->user_m->get_user_by_user_id($this->uid);
        $data['member'] = $member;
        // 送货费
        $orders = $this->tmporder_m->get_tmp_orders_by_order_no($order_no);
        if (!empty($orders)) {
            $songhuo_fee = "0";
            foreach ($orders as $key => $val) {
                $good             = $this->good_m->get_shop_goods_by_id($val['product_id']);
                $cateinfo         = $this->category_m->get_category_by_id($good['good_category']);
                $good_total_price = $val['good_mum'] * ($good['good_price'] * 100) / 100;
                $songhuo_fee      += $good_total_price * ($cateinfo['category_tax'] * 100) / 100;
            }
            $data['songhuo_fee'] = $songhuo_fee;
        }
        // 开票
        $kaipiao_fee         = $order['good_price'] * ($this->config->item('kaipiao_tax') * 100) / 100;
        $data['kaipiao_fee'] = $kaipiao_fee;
        // 服务费
        $service_fee         = $order['good_price'] * ($this->config->item('service_tax') * 100) / 100;
        $data['service_fee'] = $service_fee;
        $data['order_price'] = $order['good_price'] + $service_fee;
        // 地址
        $dress = $this->member_m->get_address_by_uid($this->uid);
        if (!empty($dress)) {
            foreach ($dress as $key => $val) {
                $area                      = $this->areas_m->get_area_by_id($val['dress_province']);
                $area2                     = $this->areas_m->get_area_by_id($val['dress_city']);
                $area3                     = $this->areas_m->get_area_by_id($val['dress_district']);
                $dress[$key]['dress_city'] = $area['area_name'] . $area2['area_name'] . $area3['area_name'];
            }
        }
        $data['dress'] = $dress;
        $this->load->view('orders/pay_order', $data);
    }

    public function pay_order_do()
    {
        if ($_POST) {
            $post        = $this->input->post();
            $order_no    = $post['order_no_hidden'];
            $pay_type    = $post['pay_type'];//1线下支付2销售贷
            $order_price = $post['order_price'];
            $is_kaipiao  = $post['is_kaipiao'];
            $is_songhuo  = $post['is_songhuo'];
            $kaipiao_fee = $post['kaipiao_fee'];
            $songhuo_fee = $post['songhuo_fee'];

            $order_info = $this->order_m->get_shop_orders_by_order_no($order_no);
            // 获取余额信息
            $user_info = $this->user_m->get_user_by_user_id($this->uid);
            if ($pay_type == 2) {
                if ($user_info['user_card_rz'] < 2) {
                    show_message('您还没有开通销售贷', site_url(X_URL . 'orders/pay_order/' . $order_no), 2);
                    exit();
                }
                if ($user_info['user_money_real'] * 100 < $order_price * 100) {
                    show_message('销售贷余额不足', site_url(X_URL . 'orders/pay_order/' . $order_no), 2);
                    exit();
                }
                $order_status = 1; // 销售贷支付订单直接成功,待发货状态
            } else {
                $order_status = 0; // 线下支付等待确认收款
            }
            $service_fee = $order_info['good_price'] * ($this->config->item('service_tax') * 100) / 100;
            $data        = [
                'order_pay'      => 1,
                'order_status'   => $order_status,
                'order_pay_type' => $pay_type,
                'is_songhuo'     => $is_songhuo,
                'is_kaipiao'     => $is_kaipiao,
                'kaipiao_fee'    => $kaipiao_fee,
                'songhuo_fee'    => $songhuo_fee,
                'service_fee'    => $service_fee,
                'order_price'    => $order_price,
                'dress_id'       => $post['dress_id'],
            ];
            $return      = $this->order_m->sava_shop_orders_by_no($order_no, $data);
            if ($return) {
                if ($pay_type == 2) {
                    // 销售贷
                    $this->load->model('moneysale_m');
                    $this->load->model('moneylogs_m');
                    $rest     = ($user_info['user_money_real'] * 100 - $order_price * 100) / 100;
                    $sale_log = [
                        'user_id'         => $this->uid,
                        'order_no'        => $order_no,
                        'order_cost'      => $order_info['order_price'],
                        'user_money_real' => $rest,
                        'ctime'           => date("Y-m-d H:i:s"),
                    ];
                    $this->moneysale_m->add_user_money_sale($sale_log);
                    // 变动日志
                    $money_log = [
                        'user_id'        => $this->uid,
                        'user_money_num' => $order_info['order_price'],
                        'type'           => 1,
                        'remark'         => "销售贷支付",
                        'ctime'          => date("Y-m-d H:i:s"),
                    ];
                    $this->moneylogs_m->add_money_logs($money_log);
                    // 更新用户表余额
                    $this->user_m->update_user($this->uid, ['user_money_real' => $rest]);
                }
                $good_data   = $this->good_m->get_good_property_by_id($order_info['good_id']);
                $message_txt = $good_data['good_title'] . $good_data['good_memo'] . " " . $order_info['good_mum'] . "个";
                $massage     = [
                    'message_txt'    => "您已经成功下单 " . $message_txt,
                    'message_to_uid' => $this->uid,
                    'message_time'   => date("Y-m-d H:i:s"),
                ];
                $this->message_m->add_massage($massage);
                show_message('恭喜您，下单成功！', site_url(X_URL . 'orders/index/'), 1);
                exit();
            }
        }
    }

    public function show_orders($id)
    {

        $order_info    = $this->order_m->get_shop_orders_by_id($id);
        $data['order'] = $order_info;
        // 分表数据
        $orders = $this->tmporder_m->get_tmp_orders_by_order_no($order_info['order_no']);
        if (!empty($orders)) {
            foreach ($orders as $key => $val) {
                $good                             = $this->good_m->get_shop_goods_by_id($val['product_id']);
                $orders[$key]['good_name']        = $good['good_title'];
                $orders[$key]['good_memo']        = $good['good_memo'];
                $orders[$key]['good_content']     = $good['good_content'];
                $orders[$key]['per_good_price']   = $good['good_price'];
                $orders[$key]['good_part_number'] = $good['good_part_number'];
                $orders[$key]['good_cover']       = $good['good_cover'];
            }
        }
        $data['order_list'] = $orders;
        // 地址
        $dress = $this->member_m->get_dress_by_dress_id($order_info['dress_id']);
        if (!empty($dress)) {
            $area                = $this->areas_m->get_area_by_id($dress['dress_province']);
            $area2               = $this->areas_m->get_area_by_id($dress['dress_city']);
            $area3               = $this->areas_m->get_area_by_id($dress['dress_district']);
            $dress['dress_city'] = $area['area_name'] . $area2['area_name'] . $area3['area_name'];
        } else {
            $dress = $this->member_m->get_dress_by_user_id($order_info['user_id']);
            // pp($dress);exit;
            $area1              = $this->areas_m->get_area_by_id($dress['dress_province']);
            $area2              = $this->areas_m->get_area_by_id($dress['dress_city']);
            $area3              = $this->areas_m->get_area_by_id($dress['dress_district']);
            $data['dress_city'] = $area1['area_name'] . $area2['area_name'] . $area3['area_name'] . $dress['dress_udress'];
        }
        $data['dress'] = $dress;
        $this->load->view('orders/order_list', $data);
    }

    public function tanchuang()
    {

        $this->load->view('orders/tanchuang');
    }

    public function add_addr()
    {

        $post                   = $this->input->post();
        $data['user_id']        = $this->uid;
        $data['dress_uname']    = $post['dress_uname'];
        $data['dress_utel']     = $post['dress_utel'];
        $data['dress_province'] = $post['province'];
        $data['dress_city']     = $post['city'];
        $data['dress_district'] = $post['district'];
        $data['dress_udress']   = $post['address'];
        $data['dress_add_time'] = time();
        $data['dress_type']     = $post['dress_type'];
        if ($post['dress_type'] == 1) {
            $data1['dress_type'] = 0;
            $this->member_m->change_dress_by_user_id($this->uid, $data1);
            $this->member_m->add_dress($data);
        } else {
            $this->member_m->add_dress($data);
        }
        $dress = $this->member_m->get_address_by_uid($this->uid);
        if (!empty($dress)) {
            foreach ($dress as $key => $val) {
                $area                      = $this->areas_m->get_area_by_id($val['dress_province']);
                $area2                     = $this->areas_m->get_area_by_id($val['dress_city']);
                $area3                     = $this->areas_m->get_area_by_id($val['dress_district']);
                $dress[$key]['dress_city'] = $area['area_name'] . $area2['area_name'] . $area3['area_name'];
            }
        }
        echo json_encode(['code' => 200, 'msg' => '新增地址成功', "data" => $dress]);
        exit();
    }

    //导出订单
    public function export_excel()
    {
        $endtime      = $this->input->get('endtime');
        $starttime    = $this->input->get('starttime');
        $end          = $endtime . " 23:59:59";
        $start        = $starttime . "  00:00:00";
        $list         = $this->tmporder_m->get_excel_orders_by_time($end, $start);
        $export_excel = [];
        if ($list) {
            foreach ($list as $k => $v) {
                $order = $this->order_m->get_shop_orders_by_order_no($v['order_no']);
                if ($order['order_status'] > 0) {
                    $good                                 = $this->good_m->get_shop_goods_by_id($v['product_id']);
                    $export_excel[$k]['user_name']        = $v['user_name'];
                    $export_excel[$k]['user_tel']         = $v['user_tel'];
                    $export_excel[$k]['order_no']         = $v['order_no'];
                    $export_excel[$k]['good_name']        = $good['good_title'];
                    $export_excel[$k]['good_memo']        = $good['good_memo'];
                    $export_excel[$k]['good_part_number'] = $good['good_part_number'];
                    //正则匹配
                    // preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $good['good_content'], $matches);
                    // $str                                = implode('', $matches[0]);
                    // $str                                = str_split($str, 3);
                    // $str                                = array_unique($str);
                    // $str                                = implode($str);
                    // $export_excel[$k]['good_content']   = $str;
                    $export_excel[$k]['good_content']   = $good['good_content'];
                    $export_excel[$k]['good_mum']       = $v['good_mum'];
                    $export_excel[$k]['good_date']      = $order['good_date'];
                    $export_excel[$k]['good_cover']      = $good['good_cover'];
                    $export_excel[$k]['order_add_time'] = date("Y-m-d H:i", $v['order_add_time']);
                }
            }
        }
        // pp($export_excel);exit();
        $filename = '订单列表' . date('YmdHis').".xls";
        $header   = ['用户名', '电话', '订单号', '产品名称', '产品规格', '产品工艺', '产品数量', '下单时间', '料号', '交货日期', '工艺图'];
        $index    = ['user_name', 'user_tel', 'order_no', 'good_name', 'good_memo', 'good_content', 'good_mum', 'order_add_time', 'good_part_number', 'good_date', 'good_cover'];
        $chr      = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j', "K"];
        $this->createtable2($export_excel, $filename, $header, $index, $chr);
    }

    protected function createtable($list, $filename, $header = [], $index = [])
    {
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:filename=" . $filename . ".xls");
        header("Expires: 0");
        header("Pragma: public");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        $teble_header = implode("\t", $header);
        $strexport    = $teble_header . "\r";
        foreach ($list as $row) {
            foreach ($index as $val) {
                $strexport .= $row[$val] . "\t";
            }
            $strexport .= "\r";
        }
        $strexport = iconv('UTF-8', "GB2312//IGNORE", $strexport);

        exit($strexport);
    }

    public function createtable2($goods, $filename, $header, $index, $chr)
    {
        // 实例化excel
        // 引入文件
        require_once(APPPATH . 'third_party/PHPExcel.php');
        require_once(APPPATH . 'third_party/PHPExcel/Writer/Excel2007.php');
        require_once(APPPATH . 'third_party/PHPExcel/IOFactory.php');
        $phpExcel   = new PHPExcel();
        $phpExcel->getProperties()->setTitle("商品价格表");
        $phpExcel->getProperties()->setSubject("趣买呗商品价格表");
        // 对单元格设置居中效果
        foreach ($chr as $ch) {
            $phpExcel->getActiveSheet()->getStyle($ch)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        }
        //单独添加列名称
        $phpExcel->setActiveSheetIndex(0);
        foreach ($header as $key => $val) {
            $phpExcel->getActiveSheet()->setCellValue($chr[$key] . '1', $val);//可以指定位置
        }
        // 设置高度
        foreach ($chr as $ch) {
            $phpExcel->getActiveSheet()->getColumnDimension($ch)->setWidth(20);
        }


        //循环添加数据（根据自己的逻辑）
        foreach ($goods as $k => $v) {
            $i = $k + 2;
            // 对样式进行修饰
            $phpExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(50);
            $phpExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $phpExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

            //  $index    = ['user_name', 'user_tel', 'order_no', 'good_name', 'good_memo', 'good_content', 'good_mum', 'order_add_time', 'good_part_number', 'good_date', 'good_cover'];

            $phpExcel->getActiveSheet()->setCellValue('A' . $i, $v['user_name']);
            $phpExcel->getActiveSheet()->setCellValue('B' . $i, $v['user_tel']);
            $phpExcel->getActiveSheet()->setCellValue('C' . $i, $v['order_no']."\t");
            $phpExcel->getActiveSheet()->setCellValue('D' . $i, $v['good_name']);
            $phpExcel->getActiveSheet()->setCellValue('E' . $i, $v['good_memo']);
            $phpExcel->getActiveSheet()->setCellValue('F' . $i, $v['good_content']);
            $phpExcel->getActiveSheet()->setCellValue('G' . $i, $v['good_mum']);
            $phpExcel->getActiveSheet()->setCellValue('H' . $i, $v['order_add_time']);
            $phpExcel->getActiveSheet()->setCellValue('I' . $i, $v['good_part_number']);
            $phpExcel->getActiveSheet()->setCellValue('J' . $i, $v['good_date']);

            // 对图片进行判断
            if (!empty($v['good_cover'])) {
                $image = FCPATH . $v['good_cover'];
                if (@fopen($image, 'r')) {
                    //这是一个坑,刚开始我把实例化图片类放在了循环外面,但是失败了,也就是每个图片都要实例化一次
                    $objDrawing = new PHPExcel_Worksheet_Drawing();
                    $objDrawing->setPath($image);
                    // 设置图片的宽度
                    $objDrawing->setHeight(65);
                    $objDrawing->setWidth(65);
                    $objDrawing->setCoordinates('K' . $i);
                    $objDrawing->setWorksheet($phpExcel->getActiveSheet());
                }
            }
        }

        // 对文件进行保存
        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$filename\"");
        header('Cache-Control: max-age=0');
        // 通过工厂类实例化excel5,本来我想使用 excel2007,但是本地测试没有问题,到线上就出错,报错链接找不到
        $objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载

    }

}
